<?

//	*****************************************
//	SbSE DB multiple rows fetch component
//	v1.1.2 / 2011.09.18
//	*****************************************

require_once E.'_lib/_db.php';

class DB_LIST {

	var $tables;	// FROM
	var $where;		// WHERE
	var $selection;	// SELECT
	var $index;		// key property for fetch
	
	var $data;			// выходная инфа по проектам из базы

	var $res;				// код ошибки	

	var $err = array (
		0 => 'success',
		1 => 'empty result',
		255 => 'not initialized'
	);
	
	function DB_LIST() {

		$this->db = $GLOBALS['db'];
		$this->tables = '';
		$this->where = defined('USE_ADODB_OCI') ? '1=1' : 1;
		$this->selection = '*';
		$this->order = '';
		$this->index = '';
		$this->group = '';
		$this->limit = '';

		$this->data = 0;
		
		$this->res = 255;
	}

	function init( $params = array() ) {

		if (@$params['table'])
			$this->tables = str_replace(';', '', trim($params['table']) );				// ! здесь надо проверять корректность куска SQL
		else
			return 0;	// error 255 - not initialized.

		if (@$params['where'])
			$this->where = str_replace(';', '', trim($params['where']) );		// ! здесь надо проверять корректность куска SQL

		if (@$params['select'])
			$this->selection = str_replace(';', '', trim($params['select']) );		// ! здесь надо проверять корректность куска SQL

		if (@$params['order'])
			$this->order = ' ORDER BY '.str_replace(';', '', trim($params['order']) );		// ! здесь надо проверять корректность куска SQL

		if (@$params['group'])
			$this->group = ' GROUP BY '.str_replace(';', '', trim($params['group']) );		// ! здесь надо проверять корректность куска SQL

		if (@$params['db'])
			$this->db = $params['db'];


		if (@$params['limit'])
			if (defined('USE_ADODB')) {
				if (is_array($params['limit'])) {
					if (is_int($params['limit'][1]))
						$this->limit = array(
							'limit' => (int)$params['limit'][1], 
							'offset' => (int)$params['limit'][0], 
						);
				} else {
					if ((int) $params['limit'])
						$this->limit = array(
							'limit' => (int)$params['limit'], 
							'offset' => -1, 
						);
				}
			} else {
				if (is_array($params['limit'])) {
					if ((int)$params['limit'][1])
						$this->limit = ' LIMIT '.((int)$params['limit'][0]).','.((int)$params['limit'][1]);
				} else {
					if ((int) $params['limit'])
						$this->limit = ' LIMIT '.((int)$params['limit']);
				}
			}

		if (@$params['index'])
			$this->index = $params['index'];

		$this->res = (defined('DBProto')) ? $this->fetch() : -1;

		return ! (bool) $this->res;
	}


	function fetch() {
	
		if (defined('USE_ADODB')) {
			$q = "SELECT {$this->selection} FROM {$this->tables} WHERE ({$this->where}) ".$this->group.$this->order;
			
			if ($this->limit) {
				$res = $this->db->SelectLimit($q, $this->limit['limit'], $this->limit['offset']);
			} else {
				$res = $this->db->Execute($q);
			}
			$this->data = array();
			
			if ($res) // returned something
				while ($d = $res->FetchRow()) {
					$t = array();
					foreach($d as $k => $v)	if (!is_int($k)) $t[strtolower($k)] = stripslashes($v); // cleanup the junky returner record
	
					if ($this->index && $t[$this->index])
						$this->data[$t[$this->index]] = $t;
					else
						$this->data[] = $t;
				}

//debug($q, 'sql', 1);				
//debug($res, 'res', 1);				
//debug($this->data, 'db_list', 1);				

		} else {
			
			$q = new SQL("SELECT {$this->selection}
						  FROM {$this->tables}
						  WHERE ({$this->where}) 
						  ".$this->group.$this->order.$this->limit );
			if ($q->run() && $q->num_rows)
				$this->data = $q->fetch($this->index);
			else 
				$this->data = array();										// empty result
		}
		return 0;		// ok
	}
	


	function error() { return $this->err[$this->res]; }

}
